<script setup lang="ts">
import { ref, onMounted, watch,computed } from 'vue'
import { useBar } from '@/hooks/chart'
import { HttpCodeEnum } from '@packages/share/enums'
import { getBarChartAPI } from '@/api/asset/index'
const emit = defineEmits(['upBardate'])
const { options } = useBar('')

const chartRef = ref()
// 选择条件
const props = defineProps({
  select: {
    type: Object,
    required: true,
    default: () => {}
  },
  country:{
    type:String,
    default:''
  }
})

const all = computed(() => {
  return false
  // return!(props.select.creatBeginTime || props.select.createEndTime || props.select.topic || props.country);
});
// 柱状图初始化
const init = () => {
  chartRef.value.loadingChart(true)
  chartRef.value.initChart()
  chartRef.value.clickChart(mapClick)
  getInfo(false)
}
// 参数合并
const _parameter = () => {
  return { ...props.select,all:all.value,country:props.country }
}
// 柱状图请求
const getInfo = (flag: boolean) => {
  getBarChartAPI(_parameter()).then((res) => {
    let { data, code } = res.data
    if (code === HttpCodeEnum.SUCCESS) {
      options.xAxis.data = data.x
      options.series[0].data = data.y
      // flag?更新|初始化
      flag ? chartRef.value.updateChart() : chartRef.value.initChart()
    }
  })
}
// 柱状图点击事件-抛出地区名
const mapClick = (e: any) => {
  emit('upBardate', e.name)
}
// 侦听选择变化
watch(props, () => {
  getInfo(true)
})
onMounted(() => {
  init()
})
</script>
<template>
  <div class="chart-content">
    <jmp-chart ref="chartRef" v-model:option="options"></jmp-chart>
  </div>
</template>
<style lang="scss" scoped></style>
